﻿@{
    ViewBag.Title = "Sign in to Admin";
}
<link href="@Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" type="text/css">

<div class="hero-unit">
    @if (ViewBag.NeedsToCreateUser)
    {
        <h1>Welcome to PrairieAsunder admin.</h1>
        <p>Before we begin, let's create an admin user. Click sign-in to create the admin user.</p>
    }
    else
    {
        <h1>Sign in required</h1>
        <p>Please sign in to access the admin page.</p>
    }
</div>
<div>
    <div class="well well-large">
        <a id="sign-in-button" class="btn-link" href="#"><img src="https://developer.mozilla.org/files/3955/email_sign_in_black.png" /></a>
    </div>
</div>

<script src="@Url.Content("~/Scripts/bootstrap.min.js")" type="text/javascript"></script>
<script src="https://login.persona.org/include.js"></script>
<script type="text/javascript">

    $(function () {

        $("#sign-in-button").click(function () {
            navigator.id.request({
                siteName: 'PrairieAsunder',
                oncancel: function () { }
            });
        });

        var currentUser = null;
            @if (User.Identity.IsAuthenticated)
            {
                <text>
                currentUser = '@User.Identity.Name';
                </text>
            }

        navigator.id.watch({
            loggedInUser: currentUser,
            onlogin: function (assertion) {
                $.ajax({
                    type: 'POST',
                    url: '/Admin/PersonaLogin', // This is a URL on your website.
                    data: { assertion: assertion },
                    success: function (res, status, xhr) {
                        window.location.href = "/admin";
                    },
                    error: function (res, status, xhr) { alert("login failure" + res); }
                });
            },
            onlogout: function () {
                // A user has logged out! Here you need to:
                // Tear down the user's session by redirecting the user or making a call to your backend.
                // Also, make that loggedInUser will get set to null on the next page load.
                // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
                $.ajax({
                    type: 'POST',
                    url: '/Admin/Logout', // This is a URL on your website.
                    success: function (res, status, xhr) { window.location.reload(); },
                    error: function (res, status, xhr) { alert("logout failure" + res); }
                });
            }
        });
    });

    

</script>